package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 服务人员认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class WorkerCertificationServiceImpl extends ServiceImpl<WorkerCertificationMapper, WorkerCertification> implements IWorkerCertificationService {

    @Resource
    private IWorkerCertificationAuditService workerCertificationAuditService;

    /**
     * 根据服务人员id更新
     *
     * @param workerCertificationUpdateDTO 服务人员认证更新模型
     */
    @Override
    public void updateById(WorkerCertificationUpdateDTO workerCertificationUpdateDTO) {
        LambdaUpdateWrapper<WorkerCertification> updateWrapper = Wrappers.<WorkerCertification>lambdaUpdate()
                .eq(WorkerCertification::getId, workerCertificationUpdateDTO.getId())
                .set(WorkerCertification::getCertificationStatus, workerCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getName()), WorkerCertification::getName, workerCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getIdCardNo()), WorkerCertification::getIdCardNo, workerCertificationUpdateDTO.getIdCardNo())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getFrontImg()), WorkerCertification::getFrontImg, workerCertificationUpdateDTO.getFrontImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getBackImg()), WorkerCertification::getBackImg, workerCertificationUpdateDTO.getBackImg())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationMaterial()), WorkerCertification::getCertificationMaterial, workerCertificationUpdateDTO.getCertificationMaterial())
                .set(ObjectUtil.isNotEmpty(workerCertificationUpdateDTO.getCertificationTime()), WorkerCertification::getCertificationTime, workerCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    /**
     * 服务端提交实名认证申请
     *
     * @param reqDTO
     */
    @Override
    public void submitApplication(WorkerCertificationAuditAddReqDTO reqDTO) {
        // 获取当前用户 , 判断当前用户是否已经提交过认证申请
        WorkerCertification workerCertification = super.getOne(new LambdaQueryWrapper<WorkerCertification>().eq(WorkerCertification::getIdCardNo, reqDTO.getIdCardNo()));
        if (workerCertification != null) {
            throw new RuntimeException("当前用户已经提交过认证申请");
        }
        // 插入认证申请
        workerCertification = new WorkerCertification();
        BeanUtil.copyProperties(reqDTO, workerCertification);
        workerCertification.setId(UserContext.currentUser().getId());
        workerCertification.setCertificationStatus(CertificationStatusEnum.PROGRESSING.getStatus());
        super.save(workerCertification);

        //2.添加管理端的认证审核记录
        WorkerCertificationAudit audit = new WorkerCertificationAudit();
        BeanUtil.copyProperties(workerCertification, audit);
        audit.setId( null);//拷贝id不能要
        audit.setServeProviderId(workerCertification.getId());//服务人员id
        workerCertificationAuditService.save(audit);
    }
    /**
     * 获取认证拒绝原因
     *
     * @return
     */
    @Override
    public Map<String, Object> rejectReason() {

        WorkerCertificationAudit audit = workerCertificationAuditService.getOne(
                Wrappers.<WorkerCertificationAudit>lambdaQuery()
                        .eq(WorkerCertificationAudit::getServeProviderId, UserContext.currentUser().getId())
                        .select(WorkerCertificationAudit::getRejectReason)
        );

        if (audit != null) {
            return Map.of("rejectReason", audit.getRejectReason());
        }
        return Map.of();

    }
}
